package com.epoint.mobileoa.personalized.epointcompany.bizlogic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Element;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Message;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.datanalysis.XMLHelp;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.db.DBOpenHelp;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.model.CodeItemsModel;

/**
 * @author liyc
 * @time 2012-4-13 下午6:48:40
 * @annotation 
 */
public class InitAddWorkContentPageInfoTask extends Task {

	public InitAddWorkContentPageInfoTask(SuperView con,
			HashMap<String, Object> param) {
		super(con, param);
	}

	@Override
	public void exectue(Message mess) throws Exception {
		String seamurl = generateURL("Epoint_AddWorkContentPageInfo");
		String bs = HttpHelp.getHttpBack(seamurl);
		List<CodeItemsModel> list = XmlAnalysis(bs);
		DBHelp.saveCode_Items(getCon(),list);
		setMessageToRefreshUI(mess, getCon(), null);
	}

	public static List<CodeItemsModel> XmlAnalysis(String xml){
		List focs = XMLHelp.getRoot(xml).elements();
		List<CodeItemsModel> plist = new ArrayList<CodeItemsModel>();
		for (int i = 0; i < focs.size(); i++) {
			CodeItemsModel mt = new CodeItemsModel();
			Element foc = (Element) focs.get(i);
			if (foc.element("ItemValue") != null)
				mt.ItemValue = foc.elementText("ItemValue");
			if (foc.element("ItemText") != null)
				mt.ItemText = foc.elementText("ItemText");
			if (foc.element("ItemID") != null)
				mt.ItemID = foc.elementText("ItemID");
			if (foc.element("CodeID") != null)
				mt.CodeID = foc.elementText("CodeID");
			if (foc.element("CodeName") != null)
				mt.CodeName = foc.elementText("CodeName");
			plist.add(mt);
		}
		return plist;
	}
	
	//获取Code_Item
	public static List<CodeItemsModel> getCodeItem_GZLX(Context con){
		DBOpenHelp dbhelper = new DBOpenHelp(con, con.getString(R.string.dbname));
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select * from code_items where CodeName = '日志明细工作类别' and length(ItemValue) =2",
				null); 
		List<CodeItemsModel> list = new ArrayList<CodeItemsModel>();
		
		while(cursor.moveToNext()){
			CodeItemsModel cim = new CodeItemsModel();
			cim.ItemValue = cursor.getString(0);
			cim.ItemText = cursor.getString(1);
			cim.ItemID = cursor.getString(2);
			cim.CodeID = cursor.getString(3);
			cim.CodeName = cursor.getString(4);
			list.add(cim);
		}
		
		DBHelp.close(dbhelper, db, cursor);
		
		return list;
	}
	
	public static int getWorkTypeCodeItemsCount(Context con,String codename){//日志明细工作类别
		DBOpenHelp dbhelper = new DBOpenHelp(con, con.getString(R.string.dbname));
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		Cursor cursor =db.rawQuery("select count(*) from code_items where CodeName = '"+codename+"'", null);
		int nums = 0;
		while(cursor.moveToNext()){
			nums = cursor.getInt(0);
		}
		DBHelp.close(dbhelper, db, cursor);
		return nums;
	}

}
